package com.sanrenzu.everybodytv.ui.recommend;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.bean.sms.BannerBean;
import com.sanrenzu.everybodytv.ui.recommend.adapter.ListViewCommendAdapter;
import com.sanrenzu.everybodytv.ui.recommend.adapter.RecyclerViewAdapter;
import com.sanrenzu.everybodytv.ui.recommend.adapter.ViewPagerAdapter;
import com.sanrenzu.everybodytv.widget.MyListView;
import com.sanrenzu.everybodytv.widget.VerticalScrollView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class RecommendFragment extends Fragment implements RecommendContract.View {

    @BindView(R.id.bannerView_recommend)
    ViewPager bannerViewRecommend;
    @BindView(R.id.recycler_recommendF_icon)
    RecyclerView recyclerRecommendFIcon;
    @BindView(R.id.ScrollView_recommend)
    VerticalScrollView ScrollViewRecommend;
    @BindView(R.id.listView_commend_other)
    MyListView listViewCommendOther;
    //声明上下文
    private Context mContext;
    //声明黄油刀
    private Unbinder bind;
    //声明适配器
    private ListViewCommendAdapter adapter_list;
    private RecommendContract.Presenter presenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        presenter =new RecommendPresenter(this);
        presenter.getRecommendBean();
        presenter = new RecommendPresenter(this);
        presenter.getRecommendBean();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_commend, container, false);
        bind = ButterKnife.bind(this, view);
        ScrollViewRecommend.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        //调用初始化监听事件
        initOther();
        return view;
    }

    //初始化监听器和监听事件
    private void initOther() {
        ScrollViewRecommend.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                presenter.getRecommendBean();
                ScrollViewRecommend.onRefreshComplete();
            }
        });

    }

    //成功。的listVIew
    @Override
    public void onGetRecommendSuccess(List list) {
        adapter_list = new ListViewCommendAdapter(mContext, list);
        listViewCommendOther.setAdapter(adapter_list);

    }

    //失败的情况
    @Override
    public void onGetRecommenddFail(String erro) {
        Toast.makeText(mContext,erro,Toast.LENGTH_SHORT).show();
    }

    //广告栏
    @Override
    public void onGetRecommendViewPager(List<BannerBean> list) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(mContext,list);
        bannerViewRecommend.setAdapter(adapter);
    }

    //原图的图标
    @Override
    public void onGetRecommendRecycler(List list) {
        recyclerRecommendFIcon.setHasFixedSize(true);
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);
        recyclerRecommendFIcon.setLayoutManager(manager);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(mContext,list,recyclerRecommendFIcon);
        recyclerRecommendFIcon.setAdapter(adapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
